package net.lab1024.sa.admin.module.business.erp.mmo.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.bas.dao.BomordDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.BomordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.*;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MmordEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MmordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MrnordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.PrfordaEntity;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MmordAddForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MmordQueryForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.form.MmordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MmordVO;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.vo.MmordaVO;
import net.lab1024.sa.admin.module.business.erp.bas.dao.GoodsDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.GoodsEntity;
import net.lab1024.sa.base.common.code.ErpErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 工单 Service
 *
 * @Author AKITA
 * @Date 2025-04-19 08:57:48
 * @Copyright AKITA
 */

@Service
public class MmordService {

    @Resource
    private MmordDao mmordDao;
    @Resource
    private MmordaDao mmordaDao;
    @Resource
    private MmordbDao mmordbDao;
    @Resource
    private GoodsDao goodsDao;
    @Resource
    private BomordDao bomordDao;
    @Resource
    private MrnordaDao mrnordaDao;
    @Resource
    private PrfordaDao prfordaDao;


    /**
     * 分页查询
     */
    public PageResult<MmordVO> queryPage(MmordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<MmordVO> list = mmordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<Long> add(MmordAddForm addForm) {
        MmordEntity mmordEntity = SmartBeanUtil.copy(addForm, MmordEntity.class);
        mmordDao.insert(mmordEntity);
        return ResponseDTO.ok(mmordEntity.getId());
    }

    /**
     * 更新
     */

    @Transactional
    public ResponseDTO<String> update(MmordUpdateForm updateForm) {
        MmordEntity mmordEntity = SmartBeanUtil.copy(updateForm, MmordEntity.class);
        if (mmordDao.updateById(mmordEntity) == 0) {
            throw new RuntimeException("数据已被更新，刷新后重试！");
        }
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> batchDelete(List<Long> idList) {
        mmordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {


        List<MrnordaEntity> entitiesa = mrnordaDao.selectList(new LambdaQueryWrapper<MrnordaEntity>().eq(MrnordaEntity::getBaseId, docEntry));

        if (!entitiesa.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在领料通知单，禁止删除！");
        }

        List<PrfordaEntity> entitiesb = prfordaDao.selectList(new LambdaQueryWrapper<PrfordaEntity>().eq(PrfordaEntity::getBaseId, docEntry));
        if (!entitiesb.isEmpty()){
            return ResponseDTO.error(ErpErrorCode.PARAM_ERROR,"存在产品送检单，禁止删除！");
        }

        mmordDao.deleteById(docEntry);
        mmordaDao.deleteById(docEntry);
        mmordbDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<MmordVO> info(Long docEntry) {
        MmordEntity mmordEntity = mmordDao.selectById(docEntry);
        MmordVO copy = SmartBeanUtil.copy(mmordEntity, MmordVO.class);
        List<MmordaEntity> mmordaEntities = mmordaDao.selectList(new LambdaQueryWrapper<MmordaEntity>().eq(true, MmordaEntity::getId, docEntry));
        List<MmordaVO> mmordaVOS = SmartBeanUtil.copyList(mmordaEntities, MmordaVO.class);
        copy.setLista(mmordaVOS);
        return ResponseDTO.ok(copy);

    }

    public ResponseDTO<MmordEntity> myInfo(Long docEntry, Long userId) {

        return ResponseDTO.ok();
    }

    public ResponseDTO<MmordEntity> batchInsert(Long docEntry) {
        return ResponseDTO.ok();
    }



    @Transactional
    public ResponseDTO<MmordEntity> unOnPlan(Long id) {
        MmordEntity mmordEntity = new MmordEntity();
        mmordEntity.setId(id);
        mmordEntity.setPlayStatus(false);
        mmordEntity.setPlayDateTime(null);
        if (mmordDao.updateById(mmordEntity) == 0){
            throw new RuntimeException("数据异常，刷新后重试！");
        }
        mmordaDao.deleteById(id);
        mmordbDao.deleteById(id);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<MmordEntity> onPlan(Long id) {

        MmordEntity mmordEntity = mmordDao.selectById(id);
        GoodsEntity goodsEntity = goodsDao.selectOne(new LambdaQueryWrapper<GoodsEntity>().eq(GoodsEntity::getGoodsNum, mmordEntity.getItemId()));
        BomordEntity bomordEntity = bomordDao.selectOne(new LambdaQueryWrapper<BomordEntity>().eq(BomordEntity::getBomId, goodsEntity.getGoodsNum()).eq(BomordEntity::getParentId, "0"));
        tree(bomordEntity, mmordEntity, 1);
        mmordEntity.setPlayStatus(true);
        mmordEntity.setPlayDateTime(LocalDateTime.now());
        mmordDao.updateById(mmordEntity);
        return ResponseDTO.ok();
    }


    private void tree(BomordEntity bomordEntity, MmordEntity mmordEntity, Integer lineNum) {

        GoodsEntity goodsEntity = goodsDao.selectOne(new LambdaQueryWrapper<GoodsEntity>().eq(GoodsEntity::getGoodsNum, bomordEntity.getBomId()));

        if (lineNum != 1) {
            MmordaEntity mmordaEntity = new MmordaEntity();
            mmordaEntity.setId(mmordEntity.getId());
            mmordaEntity.setLineNum(lineNum - 1);
            mmordaEntity.setItemId(bomordEntity.getBomId());
            mmordaEntity.setItemName(bomordEntity.getBomName());
            mmordaEntity.setQty(bomordEntity.getQty().multiply(mmordEntity.getQty()));
            mmordaEntity.setUnit(goodsEntity.getUnit());
            mmordaEntity.setSpecs(goodsEntity.getSpecs());
            mmordaDao.insert(mmordaEntity);
        }

        List<BomordEntity> bomordEntities = bomordDao.selectList(new LambdaQueryWrapper<BomordEntity>().eq(BomordEntity::getParentId, bomordEntity.getId()));
        for (BomordEntity entity : bomordEntities) {
            tree(entity, mmordEntity, ++lineNum);
        }
    }

    public ResponseDTO<MmordVO> infoUnexecute(Long id) {

        MmordVO mmordVO = mmordDao.infoUnexecute(id);


        return ResponseDTO.ok(mmordVO);
    }


}
